#!/bin/sh

debugfs_mount_point="@KEDR_TEST_DIR@/debugfs"
point_name=__kmalloc
indicator_name=periodic

point_dir=${debugfs_mount_point}/kedr_fault_simulation/points/${point_name}

sample_indicator_test_location=@sample_indicator_test_location@
sample_fsim_payload_test_location=@sample_fsim_payload_test_location@

fsim_payload_module="$sample_fsim_payload_test_location/kedr_sample_fsim_payload.ko"
indicator_module="$sample_indicator_test_location/kedr_sample_indicator.ko"

sample_target_module_name=kedr_sample_target
sample_target_script="sh @TEST_MODULES_DIR@/sample_target/kedr_sample_target"
sample_target_device="/dev/cfake0"

kedr_conf_file="./kedr.conf"
commands_file="./commands"

kedr_control_script="sh @KEDR_INSTALL_PREFIX_EXEC@/kedr"
do_commands_script="sh @TEST_SCRIPTS_DIR@/do_commands.sh"

cat > "$kedr_conf_file" << eof
module @KEDR_FAULT_SIMULATION_REF@
payload ${fsim_payload_module}
module ${indicator_module}
eof

cat > "$commands_file" << eof
on_load $kedr_control_script start "$sample_target_module_name" -f "$kedr_conf_file" || ! printf "Cannot start KEDR.\n"
on_unload $kedr_control_script stop || ! printf "Cannot stop KEDR.\n"
on_load mkdir -p "$debugfs_mount_point" || ! printf "Cannot create directory to mount debugfs to\n"
on_load mount -t debugfs debugfs "$debugfs_mount_point" || ! printf "Cannot mount debugfs.\n"
on_unload umount "$debugfs_mount_point" || ! printf "Cannot unmount debugfs.\n"
eof

# Copy and build examples first.
if ! ./build_indicator.sh $sample_indicator_test_location; then
    exit 1
fi

if ! ./build_payload.sh $sample_fsim_payload_test_location; then
    exit 1
fi

if ! $do_commands_script "$commands_file" load; then
    printf "Cannot initialize test.\n"
    exit 1
fi
# Set indicator with "always simulate fail" scenario
printf "${indicator_name} 1" > "${point_dir}/current_indicator"

if test $? -ne 0; then
    printf "Cannot set indicator for the point.\n"
    $do_commands_script "$commands_file" unload
    exit 1
fi

#Try to load target module

${sample_target_script} load

if test $? -eq 0; then
    printf "The target module was expected to fail to load with \"always\" fault simulation scenario, "
    printf "but it was successfully loaded.\n"
    ${sample_target_script} unload
    $do_commands_script "$commands_file" unload
    exit 1
fi

# Set indicator with "simulate fail every second call" scenario
printf "${indicator_name} 2" > "${point_dir}/current_indicator"

if test $? -ne 0; then
    printf "Cannot set indicator for the point again.\n"
    $do_commands_script "$commands_file" unload
    exit 1
fi

#Now target module should be successfully loaded

${sample_target_script} load

if test $? -ne 0; then
    printf "Target module should be loaded successfully with \"fail every second call\" fault simulation scenario.\n"
    $do_commands_script "$commands_file" unload
    exit 1
fi

#But writing to the device should fail

echo 123 > "$sample_target_device"
result=$?

${sample_target_script} unload

if test "$result" -eq 0; then
    printf "It was expected that writing to the device created by sample target would fail but it succeeded.\n"
    $do_commands_script "$commands_file" unload
    exit 1
fi

if ! $do_commands_script "$commands_file" unload; then
    printf "Errors occured while finalizing the test.\n"
    exit 1
fi
